package com.tfswx.ruleengine.compute.util.sm4;

class InnerSM4_Context {
    /**
     * 模式
     */
    public int mode;

    /**
     * 是否填充
     */
    public boolean isPadding;

    /**
     * 密钥串
     */
    public String secretKey;

    /**
     * 密钥组
     */
    public long[] sk;

    /**
     * 初始化向量
     */
    public String iv;

    /**
     * 初始化向量 字节数组
     */
    public byte[] ivBytes;

    public InnerSM4_Context() {
        // 密钥
        // this.secretKey = "K54K3KH423KL789K";
        this.secretKey = "401382B1407F317D";
        this.sk = new long[32];
        // 向量
        // this.iv = "3KN576CGJ5H3VV56";
        this.iv = "4B31438F51BEC20F";
        this.ivBytes = new byte[16];
        this.mode = InnerSM4Stream.SM4_ENCRYPT;
        this.isPadding = true;
    }

    public InnerSM4_Context(int mode) {
        this();
        this.mode = mode;

        byte[] ivBytes = this.iv.getBytes();
        System.arraycopy(ivBytes, 0, this.ivBytes, 0, 16);

        byte[] keyBytes = this.secretKey.getBytes();
        switch (this.mode) {
            case InnerSM4Stream.SM4_ENCRYPT:
                InnerSM4Stream.sm4_setkey_enc(this, keyBytes);
                break;
            case InnerSM4Stream.SM4_DECRYPT:
                InnerSM4Stream.sm4_setkey_dec(this, keyBytes);
                break;
            default:
                throw new RuntimeException("不支持的操作类型：" + this.mode);
        }
    }
}
